// Parameters.cpp: implementation of the Parameters class.
//
//////////////////////////////////////////////////////////////////////

#include "Parameters.h"

//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////

Parameters::Parameters()
{
	states_ptr = NULL;

	m_heuristicLowerBound  = HEURISTIC_LOWER_BOUND;
	m_heuristicHigherBound = HEURISTIC_HIGHER_BOUND;

	m_UseCutOffLowerBound =  BPMX_LOWER_BOUND;
	m_UseCutOffHigherBound = BPMX_HIGHR_BOUND;
}

Parameters::~Parameters()
{

}

void Parameters::initialize(int argc, char *argv[])
{
//	int number1;
	if(argc != 5)
	{
		printf("Failed in get_input_parameters - 4 arguments are required \n ");
		exit(0);
	} 

	m_iFirstProblem     = atoi(argv[1]); //start from
	m_nProblemsToSolve  = atoi(argv[2]); //how many

	//Open problems txt file
   	states_ptr=fopen(argv[3],"r");
    if (states_ptr==NULL)
	{
		printf ("File %s not found or File can not be opened\n",argv[3]); 
		exit(0);
	}
	else
	{
		printf ("File %s  \n",argv[3]);
	}
	
	//Open PDB file
	pdb_ptr = fopen(argv[4], "rb");
	if (pdb_ptr==NULL){printf ("File %s not found or File can not be opened\n",argv[4]); exit(0);}
	else
	{
		printf ("File %s  \n",argv[4]);
	}

	strcpy(pdbFileName,argv[4]);
	PrintInitParameters();
}


void Parameters::PrintInitParameters()
{

	printf ("for %d cases Starting from problem number %d\n",m_nProblemsToSolve,m_iFirstProblem);
	printf ("Pattern Size = %d {",PATTERNSIZE);
	printf ("}\n",PATTERNSIZE);
	printf ("*************************************\n");
}

// Function name	: void readNextProblem 
// Description	    : This function read Next Prpblem from file
// Return type		: 
// Argument         : int *out_State - the next state
// Argument         : FILE *f - input file
void Parameters::readNextProblem (int *out_State,bool bReadFromFile)           
{
	int index;                                /* index to tile positions */
	int val;
	int sTmp[SIZE];

	if (bReadFromFile == true)
	{
		fscanf (states_ptr,"%*d");  /* skip over problem number */
			printf("\nProblem  :  ");


	   for (index = 0; index < SIZE; index++)    /* for each position */
	   {
			fscanf (states_ptr,"%d", &val); /* input tile in that position */
			printf("%d ",val);
			out_State[index] = val;
		}
		printf("\n");
	}
	else
	{
		bool bByRandomMoves = false;

		int iMove,j;
		for (int i=0;i<SIZE;i++)
			out_State[i]=i;

		if (bByRandomMoves)
		{

			for (int i=0;i<140;i++)
			{
				iMove = rand()%SIZE;
				m_gameObj.MakeMove(out_State,sTmp,iMove);
				for (j=0;j<SIZE;j++)
					out_State[j]=sTmp[j];
			}
		}
		else
		{
			for (int i=SIZE-1;i>0;i--)
			{
				int loc = rand()% SIZE;
				int tmp = out_State[i];
				out_State[i] = out_State[loc];
				out_State[loc] = tmp;
			}
			

		}
	}
}

char *Parameters::getPDBFileName()
{

	return &pdbFileName[0];
}